import { createWebHistory, createRouter } from 'vue-router'

import HomeView from '../views/home/index.vue'
import Login from '../views/login.vue'
import Goods from '../views/goods.vue'
import Detail from '../views/detail.vue'
//二级路由
import Sy from '../views/home/sy.vue'
import Classify from '../views/home/classify.vue'
import Shopping from '../views/home/shopping.vue'
import My from '../views/home/my.vue'
import Order from '../views/home/order.vue'

const routes = [
    { path: '/', redirect: '/home' },
    { path: '/login', component: Login },
    { path: '/goods', component: Goods },
    { path: '/detail/:goods_id', component: Detail },
  { path: '/home',
     component: HomeView,
     children:[
        { path: '/home', redirect: '/home/classify' },
        { path: 'sy', component: Sy },
        { path: 'classify', component: Classify },
        { path: 'shopping', component: Shopping },
        { path: 'my', component: My },
        { path: 'order/:id', component: Order },
     ]
     },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})
//导航守卫
router.beforeEach((to, from,next) => {
  if(to.fullPath=='/home/my'){
    let token=localStorage.getItem('token')
    if(token){
        next()
    }else{
        next('/login')
    }
  }else{
    next()
  }
})
export default router